<?php

namespace Home\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Crypt\Password\Bcrypt;
use Zend\Session\Container;
use Zend\Http\Client;
use Zend\Db\Sql\Where;
use Zend\Paginator\Adapter\DbSelect;
use Zend\Paginator\Paginator;
use Admin\Model\Categories;
use Zend\Db\Sql\Sql;
use Admin\Model\Video;

class CategoriesTable extends AbstractTableGateway
{
    protected $table = 'tbl_categories';

    public function __construct(Adapter $adapter)
    {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Categories());

        $this->initialize();
    }


    public function getcatebyid($id)
    {
        $cate = array();
        $resultSet = $this->select(array('categories_id'=>$id));
        foreach ($resultSet as $value) 
        {
            $cate[] = $value;
        }
        return $cate;
    }
    
    public function fetchAll()
    {
         $cate = array();
        $resultSet = $this->select();
        foreach ($resultSet as $value) 
        {
            $cate[] = $value;
        }
        return $cate;
    }
     public function getbyCate($id)
    {
        $sql = new Sql($this->adapter);
         $select = $sql->select();
        $select->columns(array('name'=>'name'));
         $select->from ('tbl_categories')
                ->join('tbl_video', 'tbl_video.categories_id=tbl_categories.categories_id',array('categories_id'=>'categories_id','url'=>'url','content'=>'content','title'=>'title','video_id'=>'video_id','image'=>'image'));
         $select->where(array('tbl_categories.categories_id'=>$id,'tbl_video.status'=>'1'));
         $select->order('video_id DESC');
         $selectString = $sql->prepareStatementForSqlObject($select);
          
         $results = $selectString->execute();
         $resultSetPrototype = new ResultSet();
            $resultSetPrototype->setArrayObjectPrototype(new Video());
            //tạo ra một bộ chuyển đổi mới pagination đối tượng
            $paginatorAdapter = new DbSelect(
                // cấu hình chọn đối tượng
                $select,
                //các bộ chuyển đổi để chạy nguoc lại
                $this->getAdapter(),
                $resultSetPrototype
                );
            $Paginator =  new Paginator($paginatorAdapter);
            

            return $Paginator;
    }
}